function addZero(vNumber) { 
	return ((vNumber < 10) ? "0" : "") + vNumber;
}
 
function dateToMyDate(input) {
	var mlang = ['January', 'February', 'March ', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
	var date = new Date(input);
	var day = addZero(date.getDate());	
	var mоnth = date.getMonth(); 
	var year = date.getFullYear();
		
	var today = day + ' ' + mlang[mоnth] + ' ' + year + ' ';	
	return today;
}

$(document).ready(function(){

	var percents = [ 1.3 , 1.4 , 1.5 , 1.6 , 1.55 , 1.65 , 1.75 , 1.2 , 1.15 ];

	var current = new Date, i = 0, plus = 0;	
	
	$('.dates').each(function() {
		plus = (i > 0) ? 1 : 0;
		current.setMonth(current.getMonth() + plus);
			$(this).html(dateToMyDate(current.getTime()));
		i++;
	});
	
	$('#sum').keypress(function(event){
		var key, keyChar;
		if(!event) var event = window.event;
		
		if (event.keyCode) key = event.keyCode;
		else if(event.which) key = event.which;

		if(key==null || key==0 || key==8 || key==13 || key==9 || key==46 || key==37 || key==39 ) return true;
		keyChar=String.fromCharCode(key);
		
		if(!/\d/.test(keyChar))	return false;
	});	
	
	$('#sum').keyup(function(event){
		
		var sum = $('#sum').val();
		var prev = 0, step = 0, val = 0,last_val= 0;

		for (i = 1; i < percents.length + 1; i++) {
			prev = 0;
			step = 0;
			$('.case' + i).each(function() {
				if (step == 0) prev = sum;
								
				val = (prev * percents[i - 1]).toFixed(2);
				$(this).html( val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ') );				
				
				if (i == 2 && step == 2) {
					last_val = val;
				}							
				if (i == 2 && step > 2) {					
					$(this).html(last_val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 '))
				}	
				
				if (i == 3 && step == 5) {
					last_val = val;
				}							
				if (i == 3 && step > 5) {					
					$(this).html(last_val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 '))
				}

				if (i == 5 && step == 2) {
					last_val = val;
				}							
				if (i == 5 && step > 2) {					
					$(this).html(last_val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 '))
				}	
				
				if (i == 6 && step == 5) {
					last_val = val;
				}							
				if (i == 6 && step > 5) {					
					$(this).html(last_val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 '))
				}	

				// 40% после разморозки для депозитный 3 месяца
				if (i == 2 && step > 2) {
					val = (prev * 1.4).toFixed(2);
					$(this).html(val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 '))
				}				
				
				// 40% после разморозки для депозитный 3 месяца сверхдоходный
				if (i == 5 && step > 2) {
					val = (prev * 1.4).toFixed(2);
					$(this).html(val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 '))
				}				
				
				prev = val;
				step++;
			});		
		}
		
		
		for (i = 1; i < percents.length + 1; i++) {
			prev = 0;
			step = 0;
			$('.case' + i).each(function() {	
			
				if (step == 0) prev = sum;				
				
				if (i == 2 && step < 2) {
					val = (prev * percents[i - 2]).toFixed(2);	
					$(this).html('<span class="greyColor">' + val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ') + '</span>' )
				}		
								
				if (i == 3 && step < 5) {
					val = (prev * percents[i - 3]).toFixed(2);
					$(this).html('<span class="greyColor">' + val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ') + '</span>' )
				}
 				if (i == 4 && step < 11) {
					val = (prev * percents[i - 4]).toFixed(2);
					$(this).html('<span class="greyColor">' + val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ') + '</span>' )
				} 					
 				if (i == 5 && step < 2) {
					val = (prev * 1).toFixed(2);
					$(this).html('<span class="greyColor">' + val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ') + '</span>' )
				} 	
 				if (i == 6 && step < 5) {
					val = (prev * 1).toFixed(2);
					$(this).html('<span class="greyColor">' + val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ') + '</span>' )
				} 					
 				if (i == 7 && step < 11) {
					val = (prev * 1).toFixed(2);
					$(this).html('<span class="greyColor">' + val.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ') + '</span>' )
				} 				
				
				prev = val;
				step++;
			});		
		}		
		
		
	});		
});